package library.and.xps.com.step.view.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.PersonRankingAdapter;
import library.and.xps.com.step.adapter.PersonRankingRes;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.AnimUtil;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseFragment;
import library.and.xps.com.step.view.customview.MyListView;

public class PersonRankingFragment extends BaseFragment {
    TextView tv_today, tv_thisweek, tv_thismonth, tv_champion_name, tv_person_ranking, tv_person_step;
    ImageView iv_champion_header;
    MyListView lv_team_ranking;
    PersonRankingAdapter personRankingAdapter;
    ProgressBar progress;
    @Override
    protected int getRes() {
        return R.layout.fragment_person_ranking;
    }

    @Override
    protected void init(View view) {
        tv_today = r2v(view, R.id.tv_today);
        tv_today.setOnClickListener(this);
        tv_thisweek = r2v(view, R.id.tv_thisweek);
        tv_thisweek.setOnClickListener(this);
        tv_thismonth = r2v(view, R.id.tv_thismonth);
        tv_thismonth.setOnClickListener(this);
        tv_person_ranking = r2v(view, R.id.tv_person_ranking);
        tv_person_step = r2v(view, R.id.tv_person_step);
        progress = r2v(view,R.id.progress);
        tv_champion_name = r2v(view, R.id.tv_champion_name);
        lv_team_ranking = r2v(view, R.id.lv_team_ranking);
        iv_champion_header = r2v(view, R.id.iv_champion_header);
        personRankingAdapter = new PersonRankingAdapter(getActivity());
        lv_team_ranking.setAdapter(personRankingAdapter);

    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onResume() {
        super.onResume();
        request(1 + "");
    }

    void setChampion(PersonRankingRes updateUserInfo) {
        if (updateUserInfo == null || updateUserInfo.getList().size() <= 0) return;
        PersonRankingRes.ListBean listBean = updateUserInfo.getList().get(0);
        if(listBean==null)return;

        tv_champion_name.setText(listBean.getNickname());
        CommonUtils.setupCircleImgWithToken(getActivity(), iv_champion_header, listBean.getIco());
    }

    void selfRankingtab(PersonRankingRes updateUserInfo) {
        int selfRanking = 0;
        for (PersonRankingRes.ListBean s : updateUserInfo.getList()) {
            selfRanking++;
            if ((s.getUserid() + "").equals(getUser().getUserid())) {
                tv_person_step.setText(s.getFinishgoal() + "");
                tv_person_ranking.setText(selfRanking + "");
                break;
            }
        }
    }

    void request(String type) {
        String data = URLConstants.USER.SelectUserrankinTeam + "?teamid=" + TeamHelper.getIns().getCurT().getTeamid() + "&type=" + type;
        OkHttpClientUtils.getDataAsync(getActivity(), data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
                progress.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    PersonRankingRes updateUserInfo = new Gson().fromJson(response.getBody(), PersonRankingRes.class);
                    personRankingAdapter.setDataset(updateUserInfo.getList());
                    CommonUtils.resetListViewHeight(lv_team_ranking);
                    setChampion(updateUserInfo);
                    selfRankingtab(updateUserInfo);
                    AnimUtil.toalphaone(lv_team_ranking, 500);
                    progress.setVisibility(View.GONE);
                }
            }

            @Override
            public void onStart() {
                personRankingAdapter.clear();
                AnimUtil.toalphazero(lv_team_ranking, 500);
                progress.setVisibility(View.VISIBLE);
            }
        }, "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_thismonth:
                request(3 + "");
                break;
            case R.id.tv_thisweek:
                request(2 + "");
                break;
            case R.id.tv_today:
                request(1 + "");
                break;
        }
    }
}
